package com.sunny.excel.excel.facade;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import com.sunny.excel.constant.Constants;
import com.sunny.excel.excel.build.impl.ExcelWriterPoweredView;
import com.sunny.excel.excel.common.ExcelWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:shucq
 * @Description:
 * @Date 2019/10/25 17:40
 */
public abstract class ExcelDataDownloadable {

    @Autowired private ExcelWriter excelWriter;

    public <T> ModelAndView downloadExcel(List<T> data, String fileName, Class<T> dataClass) {
        return downloadExcel(data, fileName, Constants.XLSX_FILE_TYPE, dataClass);
    }

    public <T> ModelAndView downloadExcel(List<T> data, String fileName, String fileType, Class<T> dataClass) {
        List<T> items = data;
        if (items == null) {
            items = new ArrayList<>();
        }
        ExcelWriterPoweredView<T> view =
            new ExcelWriterPoweredView<>(excelWriter, dataClass, fileName, fileType, items);
        return new ModelAndView(view);
    }


    public <T> ModelAndView downloadExcelWithDynamicHead(List<T> data, String fileName, Class<T> dataClass) {
        return downloadExcelWithDynamicHead(data, fileName, Constants.XLSX_FILE_TYPE, dataClass, Maps.newHashMap());
    }

    public <T> ModelAndView downloadExcelWithDynamicHead(List<T> data, String fileName, Map<String, String> head,
        Class<T> dataClass) {
        return downloadExcelWithDynamicHead(data, fileName, Constants.XLSX_FILE_TYPE, dataClass, head);
    }

    public <T> ModelAndView downloadExcelWithDynamicHead(List<T> data, String fileName, String fileType,
        Class<T> dataClass, Map<String, String> dynamicHeadHead) {
        List<T> items = data;
        if (items == null) {
            items = Lists.newArrayList();
        }
        ExcelWriterPoweredView<T> view =
            new ExcelWriterPoweredView<>(excelWriter, dataClass, fileName, fileType, items, dynamicHeadHead);
        return new ModelAndView(view);
    }
}
